//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        index_stories: [],
        index_top: [],
        lastest_date: "",
        news_box: [],
        today_date: "",
        skin_style:""
    },
    onLoad: function () {
        var that = this;
        app.getSkin();
        wx.request({
            url: "https://news-at.zhihu.com/api/4/news/latest",
            success: function (res) {
                that.setData({
                    lastest_date: res.data.date,
                    skin_style: app.globalData.skin    
                })
                // 格式化时间
                var format = function (time) {
                    // console.log(time)
                    var r = time.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1年$2月$3日")
                    return r;
                };
                that.data.news_box.push(res.data)
                for (var i in that.data.news_box) {
                    that.data.news_box[i].date = format(that.data.news_box[i].date)
                }
                that.setData({
                    index_top: res.data.top_stories,
                    index_stories: res.data.stories,
                    news_box: that.data.news_box
                });
            }
        })

        //  不需要用到用户授权
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
            }
        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
    },
    onShow:function(){
        var that = this;
        that.setData({
            skin_style: app.globalData.skin
        })
    },
    to_content: function (e) {
        let content_id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../content/content?id=' + content_id,
        });
    },
    // 右下角跳转
    to_type:function(){
        wx.navigateTo({
            url: '../type/type',
        })
    },
    // 下拉刷新  
    onPullDownRefresh: function () {
        // 显示顶部刷新图标  
        wx.showNavigationBarLoading();
        var that = this;
        wx.request({
            url: 'https://news-at.zhihu.com/api/4/news/latest',
            success: function (res) {
                that.setData({
                    index_stories: res.data.stories
                });
                // 隐藏导航栏加载框  
                wx.hideNavigationBarLoading();
                // 停止下拉动作  
                wx.stopPullDownRefresh();
            }
        })
    },

    /** 
     * 页面上拉触底事件的处理函数 
     */
    onReachBottom: function () {
        var that = this;
        // 显示加载图标  
        wx.showLoading({
            title: '玩命加载中',
        })
        // 页数+1  
        wx.request({
            url: 'https://news-at.zhihu.com/api/4/news/before/' + that.data.lastest_date,
            success: function (res) {
                that.data.news_box.push(res.data);
                // 格式化时间
                var format = function (time) {
                    var r = time.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1年$2月$3日")
                    return r;
                };
                for (var i in that.data.news_box) {
                    that.data.news_box[i].date = format(that.data.news_box[i].date)
                }

                // 设置数据  
                that.setData({
                    index_stories: res.data.stories,
                    lastest_date: that.data.lastest_date - 1,
                    news_box: that.data.news_box
                })
                // 隐藏加载框  
                wx.hideLoading();
            }
        })
    },



})
